package com.hbt.orderfood.activity;

import android.graphics.Point;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.hbt.orderfood.R;
import com.hbt.orderfood.adapter.MngOrderAdapter;
import com.hbt.orderfood.adapter.UserOrderAdapter;
import com.hbt.orderfood.db.DBAdapter;
import com.hbt.orderfood.entitys.BigOrder;
import com.hbt.orderfood.entitys.Food;
import com.hbt.orderfood.entitys.Order;
import com.hbt.orderfood.entitys.User;
import com.hbt.orderfood.utils.StringUtils;
import com.noober.menu.FloatMenu;

import java.util.LinkedList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MngOrderActivity extends AppCompatActivity {

    @BindView(R.id.recycle)
    RecyclerView recycle;
    private MngOrderAdapter mngOrderAdapter;
    private Point point;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mng_order);
        ButterKnife.bind(this);
        initRecycler();
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if(ev.getAction() == MotionEvent.ACTION_DOWN){

            point = new Point();
            point.x = (int)ev.getRawX();
            point.y = (int)ev.getRawY();
        }

        return super.dispatchTouchEvent(ev);

    }

    void initRecycler() {
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recycle.setLayoutManager(layoutManager);
        mngOrderAdapter = new MngOrderAdapter(getBigOrders());
        recycle.setAdapter(mngOrderAdapter);
        mngOrderAdapter.setListener(new MngOrderAdapter.MngOrderItemClickListener() {
            @Override
            public void click(int position, BigOrder bigOrder) {
                final FloatMenu floatMenu = new FloatMenu(MngOrderActivity.this, recycle);
                floatMenu.items("查看详情", "确认发货");
                floatMenu.setOnItemClickListener(new FloatMenu.OnItemClickListener() {
                    @Override
                    public void onClick(View v, int which) {
                        if (which == 0){
                            return;
                        }
                        new DBAdapter(MngOrderActivity.this).delOrderByUserID(String.valueOf(bigOrder.getUser().getId()));
                        mngOrderAdapter.getBigOrders().remove(position);
                        mngOrderAdapter.notifyDataSetChanged();
                        Toast.makeText(MngOrderActivity.this, "已确认发货", Toast.LENGTH_SHORT).show();
                    }
                });
                floatMenu.show(point);

            }
        });
    }//initRecycler

    List<BigOrder> getBigOrders(){
        List<BigOrder> bigOrders = new LinkedList<>();
        DBAdapter dbAdapter = new DBAdapter(MngOrderActivity.this);
        List<Order> orders = dbAdapter.getAllOrders();
        for (Order order:orders){
            Food food = dbAdapter.getFoodByFoodID(String.valueOf(order.getFood_id()));
            order.setFood(food);
        }
        List<User> users = getUserFromOrder();
        for (User user:users){
            BigOrder bigOrder = new BigOrder();
            bigOrder.setUser(user);
            for (Order o:orders){
                if(o.getUser_id()==user.getId()){
                    switch (o.getFood().getType()){
                        case StringUtils.MAIN:
                            bigOrder.getMain_order().add(o);
                            break;
                        case StringUtils.VEG:
                            bigOrder.getVeg_order().add(o);
                            break;
                        case StringUtils.DRINK:
                            bigOrder.getDrink_order().add(o);
                            break;
                    }
                }
            }
            bigOrders.add(bigOrder);
        }
        return bigOrders;
    }//getBigOrders

    List<User> getUserFromOrder(){
        List<User> users = new LinkedList<>();
        DBAdapter dbAdapter = new DBAdapter(MngOrderActivity.this);
        List<Order> orders = dbAdapter.getAllOrders();
        List<String> user_ids = new LinkedList<>();
        for (Order o:orders){
            boolean has = false;
            String user_id = String.valueOf(o.getUser_id());
            for (String s:user_ids){
                if (s.equals(user_id)){
                    has = true;
                }
            }
            if(!has){
                user_ids.add(user_id);
            }
        }
        for (String user_id:user_ids){
            users.add(dbAdapter.getUserByID(user_id));
        }

        return users;
    }//getUserFromOrder

}
